/*
 * Copyright (C) 2012 Johan Boris Iantila jiantila@gmail.com && http://www.facebook.com/johanboris.iantila
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


package mg.daloji.antandroyd;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import mg.daloji.antandroyd.enums.EPhoneState;
import mg.daloji.antandroyd.enums.EnumDroid;
import mg.daloji.antandroyd.interfaces.IAntandroydListener;
import mg.daloji.antandroyd.interfaces.IAntandroydService;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Messenger;
import android.util.Log;


/**
 *
 * @author Johan Boris Iantila
 *
 */


public class StateListener implements MediaPlayer.OnBufferingUpdateListener,
MediaPlayer.OnPreparedListener,MediaPlayer.OnErrorListener,IAntandroydService{
	public static final int PROGRESS=100;
	private MediaPlayer mp;
	private Context context;
	private Service service;
	private Messenger messenger = null;
	private  boolean played=false;
	private List<IAntandroydListener> listeners = null;
	private static IAntandroydService playerStateListener; 
	public StateListener(MediaPlayer mp,Context context,Service service){
		this.mp=mp;
		this.context=context;
		this.service=service;
		this.playerStateListener=this;
		
	}
	@Override
	public void onBufferingUpdate(MediaPlayer mp, int percent) {
		Log.i("info", "onBufferingUpdate " + percent);
		if(!played){
			played=false;
			this.context.sendBroadcast(new Intent(EPhoneState.START_STREAMING.getValue()));
		}	
	}
	
	public IAntandroydService getStateListener(){
		return playerStateListener;
	}
	@Override
	public boolean onError(MediaPlayer mp, int what, int extra) {
		return false;
	}

	@Override
	public void onPrepared(MediaPlayer mp) {

		mp.start();
		
	}
	
	private void OnCompletion() {
		// TODO Auto-generated method stub
      mp.stop();
	}
	@Override
	public void addListener(IAntandroydListener listener) {
		// TODO Auto-generated method stub
		 if(listeners == null){
		        listeners = new ArrayList<IAntandroydListener>();
		    }
		    listeners.add(listener);
		
	}
	@Override
	public void removeListener(IAntandroydListener listener) {
		// TODO Auto-generated method stub
		 if(listeners != null){
		        listeners.remove(listener);
		    }
		
	}
	
	// Notification des listeners
	private void fireDataChanged(HashMap<? extends EnumDroid,Object> data){
	    if(listeners != null){
	        for(IAntandroydListener listener: listeners){
	          //  listener.dataChanged(data);
	        }
	    }
	}

}
